package logic

import (
	"context"
	"errors"
	"person/models"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *personrpc.LoginRequest) (*personrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var findUser models.User
	if err := findUser.FindUserByMobile(models.DB, in.Mobile); err != nil {
		return nil, err
	}

	if findUser.ID == 0 {
		return nil, errors.New("该账号未注册,请前往注册")
	}

	var user models.User

	if err := user.UserLogin(models.DB, in.Mobile); err != nil {
		return nil, err
	}

	if in.Password != user.Password {
		return nil, errors.New("密码输入错误,请重新输入")
	}
	return &personrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
